package com.dormitory.myt.sushecontroller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dormitory.myt.en.Employeee;
import com.dormitory.myt.en.Student;
import com.dormitory.myt.common.R;
import com.dormitory.myt.service.EmployeeeService;
import com.dormitory.myt.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

@RestController
@RequestMapping("/loging")
public class LoginController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private EmployeeeService employeeeService;
@GetMapping("/student")
    public R<Student> a(HttpServletResponse response, @RequestParam String studentid, @RequestParam String password){
    response.setHeader("Access-Control-Allow-Origin","*");
    Student student=new Student();
    student.setStudentid(studentid);
    student.setPassword(password);
    //根据studentid查询：
    LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(Student::getStudentid,student.getStudentid());
    Student s = studentService.getOne(queryWrapper);
    //判断用户名与密码：
    if(Objects.equals(s.getPassword(), password) && Objects.equals(s.getStudentid(), studentid)){

        return R.success(s);

        }
       else {

           return R.error("用户名或密码错误!");
       }

    }


    @GetMapping("/employee")
    public R<Employeee> a2(HttpServletResponse response, @RequestParam String username, @RequestParam String password){
        response.setHeader("Access-Control-Allow-Origin","*");


        LambdaQueryWrapper<Employeee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employeee::getUsername,username);
        Employeee s = employeeeService.getOne(queryWrapper);
        //判断用户名与密码：
        if(Objects.equals(s.getPassword(), password) && Objects.equals(s.getUsername(), username)){

            return R.success(s);

        }
        else {

            return R.error("用户名或密码错误!");
        }

    }
}
